﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;

namespace ScrumMagic
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class AddTab : CommonForm
    {

        //public string ProductName 
        //{
        //    get{ return ProductNameTextBox.Text;}
        //}

        //public string ProjectName
        //{
        //    get { return ProjectNameTextBox.Text; }
        //}

        //public string StartDate
        //{
        //    get { return ProductStartDateTextBox.Text; }
        //}

        //public string Status
        //{
        //    get { return ProductStatusTextBox.Text; }
        //}

        public AddTab()
        {

            InitializeComponent();
            FillStatusCombobox();
            FillProjectCombobox();
            //MessageBox.Show(checkIfExists("status","name","Dones").ToString());
        }
        private void FillProjectCombobox()
        {
            SqlCommand command = DBController.MainConnection.CreateCommand();
            command.CommandText = String.Format("Select name FROM dbo.Project");
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                ProjectCombobox.Items.Add(reader[0].ToString());
            }

            reader.Close();
        }

        private void FillStatusCombobox()
        {
            SqlCommand command = DBController.MainConnection.CreateCommand();
            command.CommandText = String.Format("Select name FROM dbo.Status");
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                StatusCombobox.Items.Add(reader[0].ToString());
            }

            reader.Close();
        }

        private int FindMaxPrId(string s)
        {
           SqlCommand commandTemp=DBController.MainConnection.CreateCommand();
            commandTemp.CommandText=String.Format("Select MAX(id) FROM dbo.{0}",s);
            SqlDataReader readerTemp= commandTemp.ExecuteReader();
            readerTemp.Read();
            int prId=0;
            Int32.TryParse(readerTemp[0].ToString(),out prId);
            readerTemp.Close();

            return prId;

        }

        private Boolean checkIfExists(string table, string column, string value)
        {
            SqlCommand comTemp = DBController.MainConnection.CreateCommand();
            comTemp.CommandText = String.Format("Select * From {1} Where {0}='{2}'", column, table, value);
            SqlDataReader readerTemp=comTemp.ExecuteReader();
            if (readerTemp.Read())
            {
                readerTemp.Close();
                return true;
            }
            else
            {
                readerTemp.Close();
                return false;
            }
        }
        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            SqlCommand cmd = DBController.MainConnection.CreateCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            SqlDataReader reader;
            
            
            int maxId = FindMaxPrId("Product");
           
            #region Version
            
            int maxVersionPrimaryId = FindMaxPrId("Version");
            bool VersionExists = checkIfExists("Version", "name", VersionTextBox.Text);            
            int VersionId=0;

            if (!VersionExists)
            {
 
                //SqlCommand cmd2 = DBController.MainConnection.CreateCommand(); ;
                //SqlDataReader reader2;
                
                cmd.CommandText = String.Format("Insert INTO dbo.Version (id,name) VALUES ('{0}','{1}')", maxVersionPrimaryId + 1, VersionTextBox.Text);
                reader = cmd.ExecuteReader();
                //reader2.Read();
                //string ProjId = reader[0].ToString();
                reader.Close();
                VersionId = maxVersionPrimaryId+1;
            }
  
            #endregion

            #region Project
            
            int maxProjectPrimaryId = FindMaxPrId("Project");
            bool projectExists = checkIfExists("Project", "name", ProjectCombobox.SelectedItem.ToString());//ProjectNameTextBox.Text);
            string ProjId=String.Empty;

            if (projectExists)
            {
                //Secelting proper Project_id
                cmd.CommandText = String.Format("Select id from dbo.Project WHERE name='{0}'", ProjectCombobox.SelectedItem.ToString());
                reader = cmd.ExecuteReader();
                reader.Read();
                ProjId = reader[0].ToString();
                reader.Close();
                //ProjId = (maxVersionPrimaryId + 1).ToString();
            }
            else
            {
                MessageBox.Show("Project Doesn't exist! Bad");
                //...
            }
            #endregion

            //Secelting proper Status_id
            cmd.CommandText = String.Format("Select id from dbo.Status WHERE name='{0}'",StatusCombobox.SelectedItem.ToString());
            reader = cmd.ExecuteReader();
            reader.Read();
            string StatId = reader[0].ToString();
            reader.Close();

            cmd.CommandText = String.Format(@"INSERT INTO dbo.Product 
 (id,Version_Id,Name,Project_id,comment,Status_id,Product.Start_Date)"
 + "VALUES  ('{0}','{1}','{2}','{3}','{4}','{5}','{6}') ", maxId + 1,VersionId,ProductNameTextBox.Text,ProjId,CommentTextBox.Text,StatId, datePicker1.SelectedDate.ToString());

            reader = cmd.ExecuteReader();   
            //reader.Read();
            //label_Name.Content = reader[0].ToString();
            reader.Close();
        }
    }
}
